package com.ttmusic.view.adapters;

import java.util.ArrayList;
import java.util.List;

import com.ttmusic.activitys.R;
import com.ttmusic.entity.MusicList;
import com.ttmusic.utils.BitmapUtil;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

/**
 * 歌单Adapter
 * @author Administrator
 *
 */
public class MusicListAdapter extends BaseAdapter{
	
	private List<MusicList> musicLists = new ArrayList<MusicList>();
	private Context mContext;
	private boolean isMainTab;
	public MusicListAdapter(Context mContext, List<MusicList> musicLists, boolean isMainTab) {
		this.mContext = mContext;
		this.musicLists = musicLists;
		this.isMainTab = isMainTab;
	}

	@Override
	public int getCount() {
		return musicLists.size();
	}

	@Override
	public MusicList getItem(int position) {
		return musicLists.get(position);
	}

	@Override
	public long getItemId(int position) {
		return position;
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		ViewHolder mViewHolder = null;
        if (convertView == null) {
            mViewHolder = new ViewHolder();
            if(isMainTab) {
            	convertView = LayoutInflater.from(mContext).inflate(R.layout.listitem_musiclist_bg_black, null);
            }else {
            	convertView = LayoutInflater.from(mContext).inflate(R.layout.listitem_musiclist_bg_white, null);
            }
            mViewHolder.musiclist_icon = (ImageView) convertView.findViewById(R.id.musiclist_icon);
            mViewHolder.musiclist_name = (TextView) convertView.findViewById(R.id.musiclist_name);
            mViewHolder.musiclist_numSongs = (TextView) convertView.findViewById(R.id.musiclist_numSongs);
            mViewHolder.musiclist_operation = (ImageView)convertView.findViewById(R.id.musiclist_operation);
            convertView.setTag(mViewHolder);
        } else {
            mViewHolder = (ViewHolder) convertView.getTag();
        }
        MusicList ml = getItem(position);
        mViewHolder.musiclist_icon.setImageBitmap(BitmapUtil.getLoacalBitmap(ml.getIconName(), mContext));
        mViewHolder.musiclist_name.setText(ml.getListName());
        mViewHolder.musiclist_numSongs.setText(ml.getNumSongs()+"首");
        mViewHolder.musiclist_operation.setOnClickListener(new View.OnClickListener() {
			@Override
			public void onClick(View v) {
				Toast.makeText(mContext, "歌单操作按钮被点击了", Toast.LENGTH_SHORT).show();
			}
		});
        return convertView;
	}
	
	final static class ViewHolder {
		ImageView musiclist_icon;
        TextView musiclist_name;
        TextView musiclist_numSongs;
        ImageView musiclist_operation;
    }

	public List<MusicList> getMusicLists() {
		return musicLists;
	}

	public void setMusicLists(List<MusicList> musicLists) {
		this.musicLists = musicLists;
	}

}
